//
//  YPCouponCell.m
//  ChengHuiTong
//
//  Created by xzm on 16/11/24.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPCouponCell.h"
#import "NSDate+Extension.h"
#import "NSString+Extension.h"

@implementation YPCouponCell{
    
    UIButton*_backGroundButton;
    
    UILabel*_rateLabel,*_packetStyle,*_caption,*_time;
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.backgroundColor = colorWithRGB(248, 249, 250);
        
        _backGroundButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _backGroundButton.frame=CGRectMake(10, 15, SCREEN_WIDTH-20,(SCREEN_WIDTH-20)*0.4);
        [self.contentView addSubview:_backGroundButton];
        
        UILabel *title= [[UILabel alloc] initWithFrame:CGRectMake(15, _backGroundButton.height*0.23, 40, 12)];
        title.text=@"加息券";
        title.textColor = [UIColor whiteColor];
        title.font = [UIFont systemFontOfSize:12];
        [_backGroundButton addSubview:title];
        
        
        _rateLabel = [UILabel creatWithFrame:CGRectMake(title.left, title.bottom+5, _backGroundButton.width/3.0-15, 40)
                                        font:[UIFont systemFontOfSize:11]
                                        text:@""
                                   textColor:[UIColor whiteColor]
                               textAlignment:0];
        [_backGroundButton addSubview:_rateLabel];
        
        
        _packetStyle=[[UILabel alloc] initWithFrame:CGRectMake(title.left, _rateLabel.bottom+5, _backGroundButton.width/3.0-15, 13)];
        _packetStyle.font=title.font;
        _packetStyle.textColor=[UIColor whiteColor];
        [_backGroundButton addSubview:_packetStyle];
        


        UILabel*explainLable=[[UILabel alloc] initWithFrame:CGRectMake(_backGroundButton.width/3.0+15, title.top, _backGroundButton.width-(_backGroundButton.width/3.0+15)-10, 12)];
        explainLable.textColor=[UIColor whiteColor];
        explainLable.font=[UIFont systemFontOfSize:11];
        explainLable.text=@"使用说明:";
        [_backGroundButton addSubview:explainLable];
        
        
        
        _caption = [[UILabel alloc] initWithFrame:CGRectMake(explainLable.left+10, explainLable.bottom+10, explainLable.width-10, 12)];
        _caption.numberOfLines=0;
        _caption.textColor = [UIColor whiteColor];
        _caption.font = [UIFont systemFontOfSize:11];
        [_backGroundButton addSubview:_caption];

        
        
        _time = [[UILabel alloc] initWithFrame:CGRectMake(_caption.left, _caption.bottom+5, _caption.width, 12)];
        _time.numberOfLines=0;
        _time.textColor = [UIColor whiteColor];
        _time.font = [UIFont systemFontOfSize:11];
        [_backGroundButton addSubview:_time];
        
        
        
    }
    
    return  self;
}
-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    if ([_model.status intValue]==1) {
        
        [_backGroundButton setBackgroundImage:[UIImage imageNamed:@"我的红包-未使用"] forState:0];
        
    }else if ([_model.status intValue]==2){
        
        [_backGroundButton setBackgroundImage:[UIImage imageNamed:@"我的红包-已使用"] forState:0];
        
        
    }else if ([_model.status intValue]==3){
        
        [_backGroundButton setBackgroundImage:[UIImage imageNamed:@"我的红包-已过期"] forState:0];
        
    }
    
    UIFont*font;
    
    if (SCREEN_WIDTH<375) {
        
        font=[UIFont systemFontOfSize:28];
        
    }else{
        
        font=[UIFont systemFontOfSize:36];
    }

    NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%%",_model.apr]];
    [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length-1)];
    _rateLabel.attributedText=string;
    
    _packetStyle.text=_model.name;
    [_packetStyle setAdjustsFontSizeToFitWidth:YES];
    if (_packetStyle.font.pointSize > 12) {
        _packetStyle.font = [UIFont systemFontOfSize:12];
    }
    
    
    _caption.text=[NSString stringWithFormat:@"%@",_model.remark];
    CGSize size=[_caption.text sizeWithFont:[UIFont systemFontOfSize:11] andMaxSize:CGSizeMake(_caption.width, 1000)];
    
    _caption.height=size.height;
    

    _time.text=[NSString stringWithFormat:@"有效日期:%@",[NSDate getDateWithInterval:[_model.end_time doubleValue] formart:@"yyyy-MM-dd HH:mm:ss"]];
    CGSize timeSize=[_time.text sizeWithFont:[UIFont systemFontOfSize:11] andMaxSize:CGSizeMake(_time.width, 1000)];
    _time.height = timeSize.height;
    _time.top=_caption.bottom+5;
}

@end
